package com.union.vehicleassistant.util;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.union.vehicleassistant.bean.ChannelBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author CuiZhen
 * @date 2019/5/18
 * GitHub: https://github.com/goweii
 */
public class SettingUtils {

    private static final String SP_NAME = "setting";
    private static final String KEY_THEME_MODE = "KEY_THEME_MODE";
    private static final String KEY_PRESSURE_UNIT = "KEY_PRESSURE_UNIT";
    private static final String KEY_PRE_SETTING_DATA = "KEY_PRE_SETTING_DATA";
    private static final String KEY_DEF_MODE_DATA = "KEY_DEF_MODE_DATA";
    private static final String KEY_CONNECT_BLE_MAC = "KEY_CONNECT_BLE_MAC";


    public enum ThemeMode {
        FOLLOW_SYSTEM(0),
        LIGHT(1),
        DARK(2);

        public final int value;

        ThemeMode(int value) {
            this.value = value;
        }

        @NonNull
        public static ThemeMode fromValue(int value) {
            for (ThemeMode themeMode : values()) {
                if (themeMode.value == value) {
                    return themeMode;
                }
            }
            return ThemeMode.FOLLOW_SYSTEM;
        }
    }

    private final SPUtils mSPUtils = SPUtils.getInstance(SP_NAME);

    private ThemeMode mThemeMode = ThemeMode.LIGHT;

    private static class Holder {
        private static final SettingUtils INSTANCE = new SettingUtils();
    }

    public static SettingUtils getInstance() {
        return Holder.INSTANCE;
    }

    private SettingUtils() {
        if (mSPUtils.contains(KEY_THEME_MODE)) {
            mThemeMode = ThemeMode.fromValue(mSPUtils.getInt(KEY_THEME_MODE, mThemeMode.value));
        }

        // 初始化预设值
        if (!mSPUtils.contains(KEY_PRE_SETTING_DATA)) {
            List<ChannelBean> channels = new ArrayList<>();
            channels.add(new ChannelBean("模式一", 10, 10));
            channels.add(new ChannelBean("模式二", 20, 20));
            channels.add(new ChannelBean("模式三", 30, 30));
            String settingData = new Gson().toJson(channels);
            mSPUtils.put(KEY_PRE_SETTING_DATA, settingData);
        }
    }

    public void setThemeMode(@NonNull ThemeMode themeMode) {
        mThemeMode = themeMode;
        mSPUtils.put(KEY_THEME_MODE, themeMode.value);
    }

    public ThemeMode getThemeMode() {
        return mThemeMode;
    }

    public void setPressureUnit(String unit) {
        mSPUtils.put(KEY_PRESSURE_UNIT, unit);
    }

    public String getPressureUnit() {
        return mSPUtils.getString(KEY_PRESSURE_UNIT, "Psi");
    }

    public void appendPreSetting(String preSettingJson) {
        String settingJson = mSPUtils.getString(KEY_PRE_SETTING_DATA, "");
        try {
            JSONArray array = new JSONArray(settingJson);
            if (preSettingJson.startsWith("{")) {
                array.put(new JSONObject(preSettingJson));
                mSPUtils.put(KEY_PRE_SETTING_DATA, new Gson().toJson(array));
            } else {
                throw new IllegalArgumentException("preSettingJson Data Must Be JsonString");
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public void setPreSettingData(String settingJson) {
        mSPUtils.put(KEY_PRE_SETTING_DATA, settingJson);
    }

    public void setPreSettingData(List<ChannelBean> settingData) {
        String json = new Gson().toJson(settingData);
        mSPUtils.put(KEY_PRE_SETTING_DATA, json);
    }

    public String getPreSettingData() {
        return mSPUtils.getString(KEY_PRE_SETTING_DATA, "");
    }

    public void setDefaultMode(ChannelBean channel) {
        if (Objects.isNull(channel)) mSPUtils.put(KEY_DEF_MODE_DATA, "");
        mSPUtils.put(KEY_DEF_MODE_DATA, new Gson().toJson(channel));
    }

    public ChannelBean getDefaultMode() {
        String json = mSPUtils.getString(KEY_DEF_MODE_DATA, "");
        return TextUtils.isEmpty(json) ? null : new Gson().fromJson(json, ChannelBean.class);
    }

    public void setConnectBleMac(String mac) {
        mSPUtils.put(KEY_CONNECT_BLE_MAC, mac);
    }

    public String getConnectBluetoothMac() {
        return mSPUtils.getString(KEY_CONNECT_BLE_MAC, "");
    }
}
